<?php
namespace app\index\Model;
use think\Model;

class TpCart extends Model
{
//$cartArr[1-6,3=>4;]goods_id-goodsattrid=>goodsnum;购物车结构
	


	//添加购物车的信息
	public function addToCart($goods_id,$goodsAttr_id='',$goodsNum=1){
		$cart=isset($_COOKIE['cart']) ? unserialize($_COOKIE['cart']) : array();
		$key=$goods_id.'-'.$goodsAttr_id;
		if(isset($cart[$key])){
			$cart[$key]+=$goodsNum;
		}else{
			$cart[$key]=$goodsNum;
		}
		
		$aMonth=time()+30*24*3600;
		setcookie('cart',serialize($cart),$aMonth,'/');
	}

	//清空购物车
	public function clearCart(){
		setcookie('cart','',1,'/');
	}

	//删除一条购物车记录
	public function delCartGoods($idAttr){
		$cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
		$key=$idAttr;
		unset($cart[$key]);
		$aMonth=time()+30*24*3600;
		setcookie('cart',serialize($cart),$aMonth,'/');
		return true;

	}

	//批量删除购物车记录
	public function delCart($cartValue){
		$cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
		$cartValue=explode('@',$cartValue);
		foreach ($cart as $k => $v) {
			if(in_array($k, $cartValue)){
				unset($cart[$k]);
			}
		}
		$aMonth=time()+30*24*3600;
		setcookie('cart',serialize($cart),$aMonth,'/');
		return true;

	}


	//获取购物车列表商品的信息
	public function getGoodsListInCart(){
		$goods=model('TpGoods');
		$cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
		$_cart=array();
		foreach ($cart as $k => $v) {
			$arr=explode('-',$k);//$arr[0]为商品的id，$arr[1]为商品属性的字符串
			$goodsInfo=$goods->field('id,goods_name,mid_thumb')->find($arr[0]);
			$memberPrice=$goods-> getMemberPrice($arr[0]);
			$_cart[$k]['goods_id']=$goodsInfo['id'];
			$_cart[$k]['goods_name']=$goodsInfo['goods_name'];
			$_cart[$k]['mid_thumb']=$goodsInfo['mid_thumb'];
			$_cart[$k]['member_price']=$memberPrice;
			$_cart[$k]['number']=$v;
			$_cart[$k]['goodsId_attrIds']=$k;
			$_cart[$k]['cart_code']=rand(111111,999999);
			$_cart[$k]['goods_attr_str']='';
			if($arr[1]){//$arr[1]为商品属性的字符串 '19,13'存在
				$goodsAttrStr=array();//商品单选属性字符串
				$goodsAttrPrice=0;
				$goodsAttrRes=db('tp_goods_attr')->alias('ga')->field('ga.*,a.attr_name')->join('tp_attr a',"ga.attr_id = a.id")->where('ga.id','in',$arr[1])->select();
				foreach ($goodsAttrRes as $k1 => $v1) {
					$goodsAttrStr[]=$v1['attr_name'].':'.$v1['attr_value'].'(￥ '.$v1['attr_price'].'元)';
					$goodsAttrPrice+=$v1['attr_price'];
				}
				$goodsAttrStr=implode('<br />', $goodsAttrStr);
				$_cart[$k]['goods_attr_str']=$goodsAttrStr;
				$_cart[$k]['member_price']+=$goodsAttrPrice;
				
			}
			$_cart[$k]['subtotal']=$_cart[$k]['member_price']*$v;
		}
		return $_cart;
	}


//选择购物车付款商品是计算价格
	public function ajaxCartGoodsAmount($recId){
		$goods=model('TpGoods');
        $_cart=array();
        $shopPrice=0;
        $memberPrice=0;
        $total=0;
        $subtotal_number=0;
        $cartIds=explode('@',$recId); //25-50,53@26-60
        $_cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
        $cartClone=$_cart;
        // 25-15,45=>2(结构)
        foreach ($cartClone as $k => $v) {
        	if(!in_array($k, $cartIds)){
        		unset($cartClone[$k]);
        	}
        }

        foreach ($cartClone as $k => $v) {
        	$subtotal_number+=$v;
        	$arr=explode('-',$k);//$arr[0]为商品的id，$arr[1]为商品属性的字符串
            $shopPrice+= $goods->getGoodsShopPrice($arr[0])*$v;
            $memberPrice+=$goods->getMemberPrice($arr[0])*$v;
            //  dump($shopPrice);
            // dump($memberPrice);die;
            if(isset($arr[1])){//$arr[1]为商品属性的字符串 '19,13'存在
                $goodsAttrPrice=0;
                $goodsAttrRes=db('tp_goods_attr')->where('id','in',$arr[1])->select();
                foreach ($goodsAttrRes as $k1 => $v1) {
                    $goodsAttrPrice+=$v1['attr_price'];
                }
                $memberPrice+=$goodsAttrPrice*$v;
                $shopPrice+=$goodsAttrPrice*$v;
            }
        }
        $goods_amount=$memberPrice;
        $save_total_amount=$shopPrice-$memberPrice;
  
        $cart=array('goods_amount'=>$memberPrice,'save_total_amount'=>$save_total_amount,'subtotal_number'=>$subtotal_number);
        return json($cart);
        //goods_amount商品总金额 ,save_total_amount优惠节省总金额 ,subtotal_number商品总数

	}


	//增加和减少购物车商品的数量
	public function UpdateCart($recId,$goodsNum){
		$cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
		$key=$recId;
		$cart[$key]=$goodsNum;
		$aMonth=time()+30*24*3600;
		setcookie('cart',serialize($cart),$aMonth,'/');
		return true;
	}

	public function count($recId,$goods_price){
		$cart=unserialize($_COOKIE['cart']);
		foreach ($cart as $k => $v) {
			if($k==$recId){
				$goods_subtotal=$goods_price*$v;
			}
		}

	}

	//要付款购物车的商品信息
	public function payGoodsInfo($recId){
		$cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
		$cartIds=explode('@',$recId);
		foreach ($cart as $k => $v) {
			if(!in_array( $k,$cartIds)){
				unset($cart[$k]);
			}
		}
		$goods=model('TpGoods');
        $_cart=array();
		foreach ($cart as $k => $v) {
			$arr=explode('-',$k);//$arr[0]为商品的id，$arr[1]为商品属性的字符串
			$goodsInfo=$goods->field('id,goods_name,mid_thumb')->find($arr[0]);
			$memberPrice=$goods-> getMemberPrice($arr[0]);
			$_cart[$k]['goods_id']=$goodsInfo['id'];
			$_cart[$k]['goods_name']=$goodsInfo['goods_name'];
			$_cart[$k]['mid_thumb']=$goodsInfo['mid_thumb'];
			$_cart[$k]['member_price']=$memberPrice;
			$_cart[$k]['number']=$v;
			$_cart[$k]['goodsId_attrIds']=$k;
			$_cart[$k]['cart_code']=rand(111111,999999);
			$_cart[$k]['goods_attr_str']='';
			if($arr[1]){//$arr[1]为商品属性的字符串 '19,13'存在
				$goodsAttrStr=array();//商品单选属性字符串
				$goodsAttrPrice=0;
				$goodsAttrRes=db('tp_goods_attr')->alias('ga')->field('ga.*,a.attr_name')->join('tp_attr a',"ga.attr_id = a.id")->where('ga.id','in',$arr[1])->select();
				foreach ($goodsAttrRes as $k1 => $v1) {
					$goodsAttrStr[]=$v1['attr_name'].':'.$v1['attr_value'].'(￥ '.$v1['attr_price'].'元)';
					$goodsAttrPrice+=$v1['attr_price'];
				}
				$goodsAttrStr=implode('<br />', $goodsAttrStr);
				$_cart[$k]['goods_attr_str']=$goodsAttrStr;
				$_cart[$k]['member_price']+=$goodsAttrPrice;

			}
            $_cart[$k]['subtotal']=$_cart[$k]['member_price']*$v;
		}
		return $_cart;

	}
//要付款购物车的商品总计
	public function payTotalInfo($recId){
		$cart=isset($_COOKIE['cart'])?unserialize($_COOKIE['cart']):array();
		$cartIds=explode('@',$recId);
		foreach ($cart as $k => $v) {
			if(!in_array( $k,$cartIds)){
				unset($cart[$k]);
			}
		}
		$goods=model('TpGoods');
        $total=array();
        $goodsTotalNum=0;
        $memberPrice=0;
		foreach ($cart as $k => $v) {
			$goodsTotalNum+=$v;
			$arr=explode('-',$k);//$arr[0]为商品的id，$arr[1]为商品属性的字符串
			$memberPrice+=$goods-> getMemberPrice($arr[0])*$v;
			if($arr[1]){//$arr[1]为商品属性的字符串 '19,13'存在
				$goodsAttrStr=array();//商品单选属性字符串
				$goodsAttrPrice=0;
				$goodsAttrRes=db('tp_goods_attr')->where('id','in',$arr[1])->select();
				foreach ($goodsAttrRes as $k1 => $v1) {
					$goodsAttrPrice+=$v1['attr_price'];
				}
				$memberPrice+=$goodsAttrPrice*$v;
				
			}
		}
		$total['goodsTotalNum']=$goodsTotalNum;
		$total['totalPrice']=$memberPrice;


		return $total;

	}






	
   
}
